package com.muhuahealth.controller;

import com.muhuahealth.entity.RoadEntity;
import com.muhuahealth.service.RoadService;
import com.muhuahealth.utils.PageQueryUtil;
import com.muhuahealth.utils.PageResult;
import com.muhuahealth.utils.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author GSK
 * @version 1.0
 * @date 2020/12/27 16:04
 */
@Slf4j
@Controller
@RequestMapping("/admin/road")
public class RoadController {

    @Autowired
    RoadService roadService;

    /**
     * 跳转到道路页面
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String roadPage(HttpServletRequest request){
        request.setAttribute("path","Basic_data_road");
        log.info("跳转到道路页面");
        return "/Basic_data_road";
    }

    /**
     * 道路数据展示列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse roadList(@RequestParam Map<String,Object> params){

        //判断分页查询是否正常，当前页码以及每页条数是否为空
       if(StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
           return ServerResponse.createError("参数异常！");
       }
       //分页查询的参数page、limit
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
       //分页的工具类
        PageResult listPage = roadService.getRoadListPage(pageQueryUtil);
        return ServerResponse.createSuccess("success",listPage);
    }

    /**
     * 新增数据
     * @param roadEntity
     * @return
     */

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse save(RoadEntity roadEntity){

        log.info("开始添加数据");
        if(StringUtils.isEmpty(roadEntity.getRoadName())
                || StringUtils.isEmpty(roadEntity.getStartName())
                || StringUtils.isEmpty(roadEntity.getEndName())
                || StringUtils.isEmpty(roadEntity.getDirection())
                || StringUtils.isEmpty(roadEntity.getOwnership())
                || StringUtils.isEmpty(roadEntity.getStreet())
                || StringUtils.isEmpty(roadEntity.getMaintenance())
                || StringUtils.isEmpty(roadEntity.getRoadLevel())
                || StringUtils.isEmpty(roadEntity.getTransectType())
                || StringUtils.isEmpty(roadEntity.getAssessLevel())){
                return ServerResponse.createError("参数异常!");
        }
        return roadService.saveRoadInfo(roadEntity);
    }

    /**
     * 查询
     * @param roadEntity
     * @return
     */

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse search(RoadEntity roadEntity){

        log.info("开始查询数据");
        if(Objects.isNull(roadEntity.getId())
            || StringUtils.isEmpty(roadEntity.getRoadName())
            || StringUtils.isEmpty(roadEntity.getStartName())
            || StringUtils.isEmpty(roadEntity.getEndName())
            || StringUtils.isEmpty(roadEntity.getDirection())
            || StringUtils.isEmpty(roadEntity.getOwnership())
            || StringUtils.isEmpty(roadEntity.getStreet())
            || StringUtils.isEmpty(roadEntity.getMaintenance())
            || StringUtils.isEmpty(roadEntity.getRoadLevel())
            || StringUtils.isEmpty(roadEntity.getTransectType())
            || StringUtils.isEmpty(roadEntity.getAssessLevel())){
            return ServerResponse.createError("参数异常！");
        }
        return roadService.searchRoadInfo(roadEntity);
    }

    /**
     * 修改
     * @param roadEntity
     * @return
     */

    @PostMapping(value = "/update")
    @ResponseBody
    public ServerResponse update(RoadEntity roadEntity){

        log.info("开始修改数据");
        if(Objects.isNull(roadEntity.getId())
                || StringUtils.isEmpty(roadEntity.getRoadName())
                || StringUtils.isEmpty(roadEntity.getStartName())
                || StringUtils.isEmpty(roadEntity.getEndName())
                || StringUtils.isEmpty(roadEntity.getDirection())
                || StringUtils.isEmpty(roadEntity.getOwnership())
                || StringUtils.isEmpty(roadEntity.getStreet())
                || StringUtils.isEmpty(roadEntity.getMaintenance())
                || StringUtils.isEmpty(roadEntity.getRoadLevel())
                || StringUtils.isEmpty(roadEntity.getTransectType())
                || StringUtils.isEmpty(roadEntity.getAssessLevel())){
            return ServerResponse.createError("参数异常!");
        }

        return roadService.update(roadEntity);
    }

    /**
     * 删除
     * @param id
     * @return
     */

    @PostMapping(value = "/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam("id") Integer id){

        log.info("开始删除数据");
        if(roadService.deleteById(id)){
            return ServerResponse.createSuccess();
        }
        return ServerResponse.createError("删除失败!");
    }
}
